package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import pojo.Student;
import service.StudentService;
import utils.Page;
import java.util.List;

@Controller
@RequestMapping("/student")

public class StudentController{
    @Autowired
    private StudentService studentService;
    @RequestMapping("/list/{currentPage}")
    public String list(Student student,@PathVariable("currentPage")Integer currentPage, Model model){
        Page page=new Page();
        page.setCurrentPage(currentPage);
        Integer totalCount = studentService.count(student);
        page.setTotalCount(totalCount);
        page.setTotalPage();
        Integer offset = (currentPage-1)*page.getPageSize();
        List<Student> students = studentService.findAll(student,offset,page.getPageSize());
        if(student.getGender()!=null){
            if(student.getGender().equals("0")){
                student.setGender("男");
            }else if(student.getGender().equals("1")){
                student.setGender("女");
            }else if(student.getGender().equals("2")){
                student.setGender("保密");
            }
        }
        model.addAttribute("students",students);
        model.addAttribute("page",page);
        model.addAttribute("student",student);
        return "/student/list";
    }
    @RequestMapping("/toadd")
    public String toAdd(){
        return "/student/add";
    }
    @RequestMapping("/addSave")
    public String add(Student student){
        int result = studentService.add(student);
        if(result>0){
            return "redirect:/student/list/1";
        }else {
            return "error";
        }
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id){
        int result = studentService.delete(id);
        if(result>0){
            return "redirect:/student/list/1";
        }else {
            return "error";
        }
    }
    @RequestMapping("/disable/{id}")
    public String disable(@PathVariable("id") Integer id){
        Student student = studentService.findById(id);
        student.setDelFlag("1");
        int result = studentService.update(student);
        if(result>0){
            return "redirect:/student/list/1";
        }else {
            return "error";
        }
    }
    @RequestMapping("/restore/{id}")
    public String restore(@PathVariable("id") Integer id){
        Student student = studentService.findById(id);
        student.setDelFlag("0");
        int result = studentService.update(student);
        if(result>0){
            return "redirect:/student/list/1";
        }else {
            return "error";
        }
    }
    @RequestMapping("/toupdate/{id}")
    public String toUpdate(@PathVariable("id") Integer id,Model model){
        Student student = studentService.findById(id);
        model.addAttribute("student",student);
        return "student/update";
    }
    @RequestMapping("/updateSave")
    public String update(Student student){
        int result = studentService.update(student);
        if(result>0){
            return "redirect:/student/list/1";
        }else {
            return "error";
        }
    }

}
